package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zq.it.pojo.Message;
import com.zq.it.service.admin.MessageService;
import com.zq.it.service.common.BaseServiceImpl;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

public class MessageServiceImpl extends BaseServiceImpl implements MessageService {

	 
	@Override
	public void removeMessage(Message msg,String realPath) {
		// TODO Auto-generated method stub
		getMessageDao().deleteMessage(msg);
		deleteFile(realPath+msg.getTitle()+FileType.TXT);			
	}
	@Override
	public void updateMessageEntity(String content,Message msg,String realPath){
      saveMsgContent(content, msg.getTitle(),realPath);
	  getMessageDao().updateMessage(msg);		  
	  // TODO Auto-generated method stu
	}

	@Override
	public List<Message> getMessagesByType(String searchId,String textValue){
		
		String  value=textValue.trim();
		String  type=searchId.trim();		
		List list=null;
		if(type.equals("0")){
			list=getMessageDao().findMessageByTitle(value);			
		}else if(type.equals("1")){
			list=getMessageDao().getMessageListByType(value);
		}else{
			list=getMessageDao().findMessageByDate(value);
		}
		return list  ;
			
		// TODO Auto-generated method stub
	}
	
	@Override
	public List getMsgByHql(String hql){
		return getMessageDao().findMsgByHql(hql);
	}
  @Override
public Message getMessageByTitle(String title){
	 return getMessageDao().findMessageByFullTitle(title);
 }
	@Override
	public List<Message> getAllMessages(){		
		// TODO Auto-generated method stub	
		return  getMessageDao().findAllMessages();
	}

	@Override
	public void addMessage(Message message,String msgContent,String realPath) {
			 getMessageDao().saveMessage(message);		
			 saveMsgContent(msgContent,message.getTitle(),realPath);		 
	}
	
	private  void saveMsgContent(String content,String fileName,String realPath){	
		writeStrContent(content,createFile(realPath,fileName,FileType.TXT));		
 }

	@Override
	public void removeMessageByGroups(String msgIdGrouds){		
		getMessageDao().deleteMessageByGroups(getCollection(getIdGroups(msgIdGrouds)));
		// TODO Auto-generated method stub		
	}
	
	@Override
	public List<Message> importMsgList(File file){
	  List<Message> msgList=getMsgListFromStr(importxlsxFile(file));
	  List<Message> existMsgList=new ArrayList<Message>();
	  
	 for(int i=0;i<msgList.size();i++){		
		 Message msg=msgList.get(i);
		 if(getMessageDao().findMessageByFullTitle(msg.getTitle())!=null)
			 existMsgList.add(msg);
	 }
	     msgList.removeAll(existMsgList);
	   if(!msgList.isEmpty())
	    	getMessageDao().saveMessageList(msgList);
		return existMsgList;
	}	
	
	
	
	 private  List<Message> getMsgListFromStr(String str){		 
		List<Message> list=new ArrayList<Message>();
		String [] firstArray=str.split(";");
		for(int i=0;i<firstArray.length;i++){
			String [] secondArray=firstArray[i].split(":");
			Message message=new Message();
			message.setTitle(secondArray[0]);
			message.setType(Integer.parseInt(secondArray[1]));
			message.setUpdateTime(secondArray[2]);	
			list.add(message);
		}
		System.out.println("大小："+list.size());
		return list;
	}
	 
	 @Override
	public ByteArrayInputStream exportMsgList(String[] colName,List list) {		
//		 System.out.println(.size());
			return new ByteArrayInputStream(exportToExcel(list,colName,HeadTitle.EN_MSGHEAD.getArray(),"com.zq.it.pojo.Message").toByteArray());		
		 }
	
	
	 
}
